/**
 * jQuery Popup Plugin v0.1
 * 
 * http://code.google.com/p/oncui/
 * http://oncui.googlecode.com/svn/trunk
 * http://www.cnblogs.com/onc4hy
 *
 * @version: 0.1 (2-JAN-2012)
 *
 * @author:onc4hy[onc4hy@gmail.com]
 *
 * $Date$ 
 * $Revision$
 *	
 * Dual licensed under the MIT and GPL licenses:
 *	http://www.opensource.org/licenses/mit-license.php
 *	http://www.gnu.org/licenses/gpl.html
 *
 * @requires jQuery v1.3.2 or later
 *
 */

/*
	Usage Note:
	-----------
	$(document).ready(function() {
		$('#button').click(function(){
			$('#popup1').popup({
				follow:'#button',
			});
			$('#popup2').popup({
				follow:'#button2',
				followPosition:'bottomleft',
			});
		});
	});
*/

(function($) {

	$.fn.popup = function(options) {
		var defaults = {
			'modal':false,
			'center':false,
			'follow':null,
			'followPosition':'bottomRight', // support:left,right,top,bottom,bottomRight,bottomLeft,topRight,topLeft
			'left':0,
			'top':0,
			'right':2,
			'bottom':2,
		};

		var settings = $.extend(defaults,options||{});

	/*
	 */
		this.each(function() {
			var target = null;
			if (settings.center != false) {
				target = $(window);
			}else if (settings.follow != null) {
				target = $(settings.follow);
			}
			if (target != null) {
				var offset = target.offset();
				var newOffset = offset;
				if(settings.followPosition == 'bottomRight') {
					newOffset.left = newOffset.left + target.outerWidth() - $(this).outerWidth();
					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}else if(settings.followPosition == 'bottomLeft') {
					newOffset.left = newOffset.left + settings.left;
					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}else if(settings.followPosition == 'topRight') {

					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}else if(settings.followPosition == 'topLeft') {

					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}else if(settings.followPosition == 'left') {
					newOffset.left = newOffset.left - $(this).outerWidth() - settings.right;
					newOffset.top = newOffset.top + settings.top;
				}else if(settings.followPosition == 'right') {
					newOffset.left = newOffset.left + target.outerWidth() + settings.left;
					newOffset.top = newOffset.top + settings.top;
				}else if(settings.followPosition == 'top') {

					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}else if(settings.followPosition == 'down') {

					newOffset.top = newOffset.top + target.outerHeight() + settings.top;
				}
			}else{
			}
			$(this).css({
				'position':'absolute',
				'left':newOffset.left,
				'top':newOffset.top,
			});
//			$(this).appendTo($(document));
			$(this).toggle();
		});
	};

})(jQuery);

